import 'package:enterprise_v6_app/app/components/common/custom_text_widget.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_intrinsic_circle_line.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/process_arrange_record_entity.dart';

/// 敏捷应急处-审批流程页面
class EmergencyApprovalProcessWidget extends StatefulWidget {
  const EmergencyApprovalProcessWidget({super.key, this.nodeList, this.isReject = false});

  // 审批流程节点数据
  final List<ProcessArrangeRecordEntity>? nodeList;

  // 是不是驳回状态
  final bool isReject;

  @override
  State<EmergencyApprovalProcessWidget> createState() => _EmergencyApprovalProcessWidgetState();
}

class _EmergencyApprovalProcessWidgetState extends State<EmergencyApprovalProcessWidget> {
  @override
  Widget build(BuildContext context) {
    if (widget.nodeList == null || widget.nodeList!.isEmpty) {
      return Container(
        alignment: Alignment.center,
        color: const Color(0xffffffff),
        child: Text(
          '-- 暂无数据 --',
          style: TextStyle(
            fontSize: 14.w,
            height: 1.2,
            color: const Color(0xff4D5956),
            fontWeight: FontWeight.w400,
          ),
        ),
      );
    }
    return Container(
      alignment: Alignment.topCenter,
      color: const Color(0xffffffff),
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 16.w),
      child: SingleChildScrollView(
        child: Column(
            children: widget.nodeList!.asMap().keys.map((index) {
          final ProcessArrangeRecordEntity item = widget.nodeList![index];
          return CustomIntrinsicCircleLine(
            iconSpaceHeight: 0.w,
            iconWidget: index != widget.nodeList!.length - 1
                ? SvgPicture.asset(
                    AppImages.icon_process_pass,
                    width: 28.w,
                    height: 28.w,
                  )
                : Container(
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: const Color(0xff5C6DFF),
                      borderRadius: BorderRadius.circular(8.r),
                    ),
                    width: 28.w,
                    height: 28.w,
                    child: Text(
                      '${index + 1}',
                      style: TextStyle(
                        fontSize: 14.sp,
                        height: 1.2,
                        fontWeight: FontWeight.w400,
                        color: const Color(0xffffffff),
                      ),
                    ),
                  ),
            showWidget: Container(
              margin: EdgeInsets.only(bottom: 20.w),
              child: _getNodeView(index: index, item: item),
            ),
          );
        }).toList()),
      ),
    );
  }

  /// 流程节点样式
  Widget _getNodeView({required int index, required ProcessArrangeRecordEntity item}) {
    if (index == 0) {
      return _getStartNodeView(index: index, item: item);
    } else {
      return _getProcessNodeView(index: index, item: item);
    }
  }

  /// 流程节点开始样式
  static Widget _getStartNodeView({required int index, required ProcessArrangeRecordEntity item}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 16.w),
      decoration: BoxDecoration(
        color: const Color(0xffffffff),
        border: Border.all(
          color: const Color(0xffDFE5FD),
          width: 1.w,
        ),
        borderRadius: BorderRadius.circular(4.r),
      ),
      child: Column(
        children: [
          CustomTextWidget.getTextRowView(title: '提交', value: item.approvalPersonnel),
          AppGaps.vGap12,
          CustomTextWidget.getTextRowView(title: '提交时间', value: item.approvalDate),
        ],
      ),
    );
  }

  /// 流程节点样式
  static Widget _getProcessNodeView({required int index, required ProcessArrangeRecordEntity item}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 16.w),
      decoration: BoxDecoration(
        color: const Color(0xffffffff),
        border: Border.all(
          color: const Color(0xffDFE5FD),
          width: 1.w,
        ),
        borderRadius: BorderRadius.circular(4.r),
      ),
      child: Column(
        children: [
          CustomTextWidget.getTextRowView(title: '审批人', value: item.approvalPersonnel),
          AppGaps.vGap12,
          _getSignWidget(title: '签字笔迹', signUrl: item.approvalSign),
          AppGaps.vGap12,
          CustomTextWidget.getTextRowView(title: '审批时间', value: item.approvalDate),
        ],
      ),
    );
  }

  /// 签字显示
  static Widget _getSignWidget({required String title, required String signUrl}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text(
          title,
          style: TextStyle(
            fontSize: 13.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: const Color(0xffA5A6B5),
          ),
        ),
        const Spacer(),
        Container(
          width: 76.w,
          height: 36.w,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(4.w),
              color: Colors.white,
              border: Border.all(
                color: const Color(0xFFEFF2F6),
                width: 1,
              ),
              image: DecorationImage(
                image: NetworkImage(Interface.fileUrl + signUrl),
              )),
        ).clickShowLarge(url: Interface.fileUrl + signUrl)
      ],
    );
  }
}
